//
//  SCVisitorView.swift
//  weibo
//
//  Created by 阿城 on 15/12/6.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit
import SnapKit

class SCVisitorView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    var loginClosrue : (()->())?
    var registerClosrue: (()->())?
    
    
    private lazy var home = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var circel = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var textLab: UILabel = {
       let lab = UILabel()
        lab.text = "关注一些人,回这里看看有惊喜关注一些人,回这里看看有惊喜"
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(15)
        lab.textAlignment = .Center
        return lab
    }()
    private lazy var rightBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        btn.addTarget(self, action: "registerClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    private lazy var leftBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.addTarget(self, action: "loginClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    
    @objc private func registerClick(){
        registerClosrue?()
    }
    @objc private func loginClick(){
        loginClosrue?()
    }
    
    
    func setupUI(){
        
        addSubview(circel)
        addSubview(mask)
        addSubview(home)
        addSubview(textLab)
        addSubview(rightBtn)
        addSubview(leftBtn)
        
        home.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        circel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(home.snp_center)
        }
        textLab.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(home.snp_centerY).offset(60)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(225)
        }
        leftBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLab.snp_leading)
            make.top.equalTo(textLab.snp_bottom).offset(20)
            make.height.equalTo(40)
        }
        rightBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(textLab.snp_trailing)
            make.top.equalTo(textLab.snp_bottom).offset(20)
            make.height.equalTo(40)
            make.width.equalTo(leftBtn.snp_width)
            make.left.equalTo(leftBtn.snp_right).offset(20)
        }
        mask.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(leftBtn.snp_bottom)
        }
    }
    
    func startAnimat(){
        let animat = CABasicAnimation()
        animat.keyPath = "transform.rotation"
        animat.toValue = 2 * M_PI
        animat.repeatCount = MAXFLOAT
        animat.duration = 10
        animat.removedOnCompletion = false
        circel.layer.addAnimation(animat, forKey: "circel")
    }

    func setDetail(imgName: String?, text: String?){
        
        guard let name = imgName, title = text else{
            startAnimat()
            return
        }
        
        home.image = UIImage(named: name)
        textLab.text = title
        circel.hidden = true
        
    }
}
